import { CoolController, BaseController, CoolUrlTag, TagTypes } from '@cool-midway/core';
import { ArticlesEntity } from '../../entity/articles';
import { Body, Get, Inject, Post, Query } from '@midwayjs/core';
import { ArticlesService } from '../../service/articles';

/**
 * 文章
 */
@CoolUrlTag({ key: TagTypes.IGNORE_TOKEN, value: ['indexList', 'detail', 'commentList', 'circleList', 'userList'] })
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: ArticlesEntity,
})
export class ArticlesController extends BaseController {
  @Inject()
  articlesService: ArticlesService;

  /**
   * 发布文章
   * @param content 内容
   * @param images 图片
   * @param circleId 圈子
   * @param tagIds 标签
   */
  @Post('/publish', { summary: '发布文章' })
  async publish(@Body() params) {
    const { content, images, circleId, tagIds } = params;
    const { success, message, data } = await this.articlesService.publish(content, images, circleId, tagIds);
    if (!success) return this.fail(data, message);
    return this.ok(data, message);
  }

  /**
   * 主页文章列表
   * @param page 页码
   * @param size 条数
   * @param type 类型 0最新 1最热
   */
  @Post('/indexList', { summary: '文章列表' })
  async indexList(@Body() params) {
    const { page, size, type } = params;
    const { success, message, data } = await this.articlesService.indexList(page, size, type);
    if (!success) return this.fail(data, message);
    return this.ok(data, message);
  }

  /**
   * 圈子文章列表
   * @param circleId 圈子id
   * @param page 页码
   * @param size 条数
   */
  @Post('/circleList', { summary: '圈子文章列表' })
  async circleList(@Body() params) {
    const { circleId, page, size } = params;
    const { success, message, data } = await this.articlesService.circleList(circleId, page, size);
    if (!success) return this.fail(data, message);
    return this.ok(data, message);
  }

  /**
   * 用户文章列表
   * @param userId 用户id
   * @param page 页码
   * @param size 条数
   */
  @Post('/userList', { summary: '用户文章列表' })
  async userList(@Body() params) {
    const { userId, page, size } = params;
    const { success, message, data } = await this.articlesService.userList(userId, page, size);
    if (!success) return this.fail(data, message);
    return this.ok(data, message);
  }

  /**
   * 文章详情
   * @param id 文章id
  */
  @Get('/detail', { summary: '文章详情' })
  async detail(@Query('id') id) {
    const { success, message, data } = await this.articlesService.detail(id);
    if (!success) return this.fail(data, message);
    return this.ok(data, message);
  }

  /**
   * 文章点赞
   * @param id 文章id
   */
  @Post('/like', { summary: '文章点赞' })
  async like(@Body() params) {
    const { id } = params;
    const { success, message, data } = await this.articlesService.like(id);
    if (!success) return this.fail(data, message);
    return this.ok(data, message);
  }

  /**
   * 文章评论
   * @param id 文章id
   * @param content 评论内容
   */
  @Post('/comment', { summary: '文章评论' })
  async comment(@Body() params) {
    const { id, content } = params;
    const { success, message, data } = await this.articlesService.comment(id, content);
    if (!success) return this.fail(data, message);
    return this.ok(data, message);
  }

  /**
   * 回复评论
   * @param toUserId 回复用户id
   * @param pid 父id
   * @param content 评论内容
   */
  @Post('/reply', { summary: '回复评论' })
  async reply(@Body() params) {
    const { toUserId, pid, content } = params;
    const { success, message, data } = await this.articlesService.reply(toUserId, pid, content);
    if (!success) return this.fail(data, message);
    return this.ok(data, message);
  }

  /**
   * 评论列表
   * @param id 文章id
   */
  @Get('/commentList', { summary: '评论列表' })
  async commentList(@Query('id') id) {
    const { success, message, data } = await this.articlesService.commentList(id);
    if (!success) return this.fail(data, message);
    return this.ok(data, message);
  }
}
